package com.vickw.springcloud.controller;

import com.vickw.springcloud.entity.CommonResult;
import com.vickw.springcloud.entity.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Description：
 * @Author VickW
 * @Date 2022/2/18 21:13
 */
@RestController
@Slf4j
public class OrderController {
    // 远程调用的 地址,这里的的路径需要加上http://,不然识汇报url异常
    // public static final String PAYMENT_URL = "http://localhost:8001"; // http://localhost:8001

    public static final String PAYMENT_URL = "http://CLOUD-PROVIDER-PAYMENT";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> createPayment(Payment payment) {
        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
    }

    // 这里要把id传到路径中去
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable Long id) { // @PathVariable
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }

    @GetMapping(value = "/consumer/payment/getEntity/{id}")
    public CommonResult<Payment> getPaymentById2(@PathVariable Long id){
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
        if (entity.getStatusCode().is2xxSuccessful()){
            log.info("*********entity.getBody()***********"+entity.getBody().toString()+"\n");
            log.info("*********entity.getStatusCode()***********"+entity.getStatusCode().toString()+"\n");
            log.info("*********entity.getStatusCodeValue()***********"+entity.getStatusCodeValue()+"\n");
            log.info("*********entity.getHeaders()***********"+entity.getHeaders().toString()+"\n");
            return entity.getBody();
        }else {
            return new CommonResult(444,"操作失败");
        }

    }


}
